import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpRequest } from "../../../../service/httpRequest";
import { NotiService } from "../../../../service/noti.service";

declare var require:any;
var Base64 = require('../../../../../assets/js/Base64');
declare let $:any;
@Component({
  selector: 'app-single-monitor-jgylx',
  templateUrl: './single-monitor-jgylx.component.html',
  styleUrls: ['./single-monitor-jgylx.component.css']
})
export class SingleMonitorJgylxComponent implements OnInit {

  deviceId:string = '';
  private gizwitsNotiWs:any;
  private eventType = ['device.status.kv'];

  constructor(private activeRoute:ActivatedRoute,private httpRequest:HttpRequest,private noti:NotiService) { }

  ngOnInit() {
    this.activeRoute.params.subscribe(
      params=>{
        this.deviceId = params['deviceId'].replace('|','/');
        let p = this.deviceId.split('/');
        this.gizwitsNotiWs = this.noti.init(p[0].toLocaleLowerCase(),[p[1]],this.eventType,this.onEvent,this);
        this.gizwitsNotiWs.connect();
      }
    );
  }
  change(i){
    let cboxs = $('.xx-box');
    if(i >= cboxs.length) return ;
    $.each(cboxs,function(j,n){
      $(n).hide();
      $('.nav-title').eq(j).removeClass('active')
    });
    $('.xx-box').eq(i).show();
    $('.nav-title').eq(i).addClass('active');
  }


  getInfo(){
    let p = this.deviceId.split('/');
    this.httpRequest.sendPost('common/sendCommand',{
      product_id:p[0],
      mac:p[1],
      command:"{\"attrs\":{\"DP01\":1}}"
    }).subscribe(	res => console.log(res),err =>{})
  }

  onEvent(msg){

    if(msg.event_type=="device_status_raw"){
      let tdata = Base64.Base64.decode(msg.data.raw);
      let zdata = JSON.parse(tdata);
      console.log(zdata.hello);

    }
  }
}
